<template>
  <el-dropdown trigger="hover" @command="changeLanguage">
    <div>
      <svg-icon
        style="color: #fff; font-size: 20px; cursor: pointer"
        icon-class="language"
      />
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item command="zh" :disabled="'zh' === $i18n.locale">
        中文
      </el-dropdown-item>
      <el-dropdown-item command="en" :disabled="'en' === $i18n.locale">
        en
      </el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
import Cookie from 'js-cookie'
export default {
  methods: {
    changeLanguage (lang) {
      // 记录
      Cookie.set('lang', lang)
      const expires = new Date(Date.now() + 1 * 60 * 1000) // 1分钟后过期
      Cookie.set('test', lang, { expires })

      this.$i18n.locale = lang // 设置给本地的i18n插件
      this.$message.success('切换多语言成功')
    }
  }
}
</script>
